
package todolist.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import todolist.personalization.DateTimePatternCustomizer;;

/**
 * Date-related utilities. All methods here are static.
 * @author Ivan
 */
public class DateUtils {
	
	/**
	 * Returns a copy of specified Date with s,millis all set to 0.
	 * @throws IllegalArgumentException
	 */
	public static Date removeSecondsFromDate(Date pdtmInput) throws IllegalArgumentException {
		if (pdtmInput == null)
			throw new IllegalArgumentException("Date input must not be null.");
		GregorianCalendar objGC = new GregorianCalendar();
		objGC.setLenient(false);
		objGC.setTime(pdtmInput);
		objGC.clear(Calendar.SECOND);
		objGC.clear(Calendar.MILLISECOND);
		return objGC.getTime();
	}
	
	/**
	 * Returns a copy of specified Date with h,m,s,millis all set to 0.
	 * @throws IllegalArgumentException
	 */
	public static Date removeTimeFromDate(Date pdtmInput) throws IllegalArgumentException {
		if (pdtmInput == null)
			throw new IllegalArgumentException("Date input must not be null.");
		GregorianCalendar objGC = new GregorianCalendar();
		objGC.setLenient(false);
		objGC.setTime(pdtmInput);
		objGC.set(Calendar.HOUR_OF_DAY, 0);
		objGC.clear(Calendar.MINUTE);
		objGC.clear(Calendar.SECOND);
		objGC.clear(Calendar.MILLISECOND);
		return objGC.getTime();
	}

	/**
	 * Returns String of formatted Date without time.
	 * @throws IllegalArgumentException
	 */
	public static String formatDateOnly(Date pdtmInput) throws IllegalArgumentException {
		if (pdtmInput == null)
			throw new IllegalArgumentException("Date input must not be null.");
		SimpleDateFormat objSDF = new SimpleDateFormat(DateTimePatternCustomizer.getInstance().getDisplayDateOnlyPattern());
		return objSDF.format(pdtmInput);
	}
	
	/**
	 * Returns String of formatted Date with time.
	 * @throws IllegalArgumentException
	 */
	public static String formatDateTime(Date pdtmInput) throws IllegalArgumentException {
		if (pdtmInput == null)
			throw new IllegalArgumentException("Date input must not be null.");
		SimpleDateFormat objSDF = new SimpleDateFormat(DateTimePatternCustomizer.getInstance().getDisplayDateTimePattern());
		return objSDF.format(pdtmInput);
	}
	
	/**
	 * Returns String of formatted Date, primarily for testing purposes.
	 * @throws IllegalArgumentException
	 */
	public static String formatFullDate(Date pdtmInput) throws IllegalArgumentException {
		if (pdtmInput == null)
			throw new IllegalArgumentException("Date input must not be null.");
		SimpleDateFormat objSDF = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
		return objSDF.format(pdtmInput);
	}

}
